<?php
function OpenContent($name,$dir,&$content)
{
	$fname=$dir.$name.".txt";
	if(file_exists($fname))
	{
		$fp = fopen($fname,"r");
		$nm=filesize($fname);
		$content=$nm?fread($fp, $nm):"";
		fclose($fp);
	}//else echo "(Error:no such content:{$fname})";

}

function SaveSite()
{
	global $section_tbl;
	global $news_tbl;

for($tbl=0;$tbl<2;$tbl++)
{
	$res=$tbl?mysql_query("select*from $news_tbl"):mysql_query("select*from $section_tbl");
	$tmp="";
	while(1)
	{
		$fl=mysql_fetch_array($res);
		if(!$fl)break;
		if($tmp!="")$tmp.=',';
		$tmp.='("'.$fl['id'].'","'.($tbl?$fl['date']:$fl['parentID']).'","'.$fl['title'].'","'.$fl['icon'].'")';
	}
	$fp = fopen("data/my_backup".$tbl.".txt","w");
	fwrite($fp, $tmp);
	fclose($fp);
}
}


function LoadSite()
{
	global $section_tbl;
	global $news_tbl;

for($tbl=0;$tbl<2;$tbl++)
{
	$fname="data/my_backup".$tbl.".txt";
	if(file_exists($fname))
	{
		$fp = fopen($fname,"r");
		$nm=filesize($fname);
		$tmp=$nm?fread($fp, $nm):"";
		$table_name=($tbl?$news_tbl:$section_tbl);
		fclose($fp);
		if($tmp!="")
		{
			mysql_query("DROP TABLE {$table_name}");
			
			if($tbl)	mysql_query("CREATE TABLE IF NOT EXISTS $news_tbl (id int(4) NOT NULL auto_increment, title varchar(255) NOT NULL, date date NOT NULL,icon varchar(255),  PRIMARY KEY (id), INDEX(title));");
				else mysql_query("CREATE TABLE IF NOT EXISTS $section_tbl (id int(4) NOT NULL auto_increment, parentID int(4) NOT NULL DEFAULT 0 ,  title varchar(255) NOT NULL,icon varchar(255),  PRIMARY KEY (id), INDEX(title));");

			mysql_query("insert into {$table_name} values ".$tmp);
		}
	}else echo "(Error:no such file123)";
}
}
?>